﻿using System;
using System.Collections.Generic;
using System.Linq;
using LumenLiquid.Database.Entities;
using LumenLiquid.Database.Entities.Settings;
using LumenLiquid.Debug;
using NHibernate;

namespace LumenLiquid.Database
{
    /// <summary>
    /// Manages all Settings for the skelleton of the game like resolution etc.
    /// </summary>
    public sealed class Properties
    {
        private readonly ISessionFactory _sessionfactory;
        private IEnumerable<Property> _settings;

        /// <summary>
        /// Creates a new Settings Instance
        /// </summary>
        public Properties(ISessionFactory sessionFactory)
        {
            _sessionfactory = sessionFactory;
        }

        #region MainSettings
        /// <summary>
        /// Reads all settings
        /// </summary>
        public void LoadSettings()
        {
            ISession session = _sessionfactory.OpenSession();

            _settings = DatabaseInteraction.ReadAllObjects<Property>(session);
            session.Close();
        }

        /// <summary>
        /// Removes all Settings
        /// </summary>
        /// <param name="save">Whether to save changed settings or not</param>
        public void DisposeSettings(bool save)
        {
            if (save)
                SaveSettings();

            _settings = null;
        }

        /// <summary>
        /// Saves the settings
        /// </summary>
        public void SaveSettings()
        {
            ISession session = _sessionfactory.OpenSession();

            DatabaseInteraction.SaveObject(session, _settings);
        }

        /// <summary>
        /// Searches a setting and grands edit and reading rights
        /// </summary>
        /// <param name="key">The key of the setting to search</param>
        /// <returns>Setting's value or null when not found</returns>
        public string this[int key]
        {
            get
            {
                try
                {
                    return _settings.Where(x => x.Id == key).ElementAt(0).SettingValue;
                }
                catch (ArgumentNullException)
                {
                    DebugHandler.Log("The setting \"" + key + "\" was not found in the settings database", DebugHandler.EDebugLevel.Warning);
                    return null;
                }
                catch(ArgumentOutOfRangeException)
                {
                    DebugHandler.Log("The setting \"" + key + "\" was not found in the settings database", DebugHandler.EDebugLevel.Warning);
                    return null;
                }
            }
            set
            {
                try
                {
                    _settings.Where(x => x.Id == key).ElementAt(0).SettingValue = value;
                }
                catch (ArgumentNullException)
                {
                    DebugHandler.Log("The setting \"" + key + "\" was not found in the settings database", DebugHandler.EDebugLevel.Warning);
                }
            }
        }

        #endregion
    }
}
